﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using famails.dto;
using famails.common;

namespace famails.dao
{

    /// <summary>
    /// Summary description for AccountLimitDAO
    /// </summary>
    public class AccountLimitDAO
    {
        public AccountLimitDAO()
        {
        }
        public DataTable GetAll()
        {
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT limitid id, cost, under, cast(limitid as varchar(100))+'-'+cast(cost as varchar(100)) limitid, nameAccountlimit FROM tblAccountLimit order by under",
                ConnectionData._MyConnection);
            DataTable table = new DataTable();
            adapter.Fill(table);
            adapter.Dispose();
            return table;
        }
        public void tblAccountLimit_insert(AccountLimitDTO dt)
        {
            ConnectionData.OpenMyConnection();
            string sql = "INSERT INTO tblAccountLimit (under,cost,isActive, IsUnLimit) " +
                         "VALUES(  @under,@cost,@isActive, @IsUnLimit) ";
            SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@under", SqlDbType.Int).Value = dt.under;
            cmd.Parameters.Add("@cost", SqlDbType.Float).Value = dt.cost;
            cmd.Parameters.Add("@isActive", SqlDbType.Bit).Value = dt.isActive;
            cmd.Parameters.Add("@IsUnLimit", SqlDbType.Bit).Value = dt.isUnLimit;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            ConnectionData.CloseMyConnection();
        }
        public void tblAccountLimit_Delete(int limitId)
        {
            ConnectionData.OpenMyConnection();
            SqlCommand cmd = new SqlCommand("DELETE FROM tblAccountLimit WHERE limitId = @limitId",
                ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@limitId", SqlDbType.Int).Value = limitId;
            cmd.ExecuteNonQuery();
            ConnectionData.CloseMyConnection();
            cmd.Dispose();
        }
        public DataTable GetByUserIdAccountLimit(int limitId)
        {
            ConnectionData.OpenMyConnection();
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblAccountLimit WHERE limitId = @limitId",
                ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@limitId", SqlDbType.Int).Value = limitId;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            ConnectionData.CloseMyConnection();
            return table;
        }
        public void tblAccountLimit_Update(AccountLimitDTO dt)
        {
            string sql = "UPDATE tblAccountLimit SET " +
                    "under = @under, " +
                    "cost = @cost, " +
                    "isActive = @isActive, " +
                    "isUnlimit = @IsUnLimit " +
                    " WHERE limitId = @limitId";
            SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@under", SqlDbType.Int).Value = dt.under;
            cmd.Parameters.Add("@cost", SqlDbType.Float).Value = dt.cost;
            cmd.Parameters.Add("@isActive", SqlDbType.Bit).Value = dt.isActive;
            cmd.Parameters.Add("@limitId", SqlDbType.Int).Value = dt.limitId;
            cmd.Parameters.Add("@IsUnLimit", SqlDbType.Bit).Value = dt.isUnLimit;
            ConnectionData.OpenMyConnection();
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            ConnectionData.CloseMyConnection();
        }
        public DataTable GetAvailableAccount(int limitId)
        {
            ConnectionData.OpenMyConnection();
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblAccountLimit except Select * From tblAccountLimit Where limitId = @limitId",
                ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@limitId", SqlDbType.Int).Value = limitId;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            ConnectionData.CloseMyConnection();
            return table;
        }
        public DataTable viladate_Accountlimint(string under, object id)
        {
            string sql = "select * from tblAccountLimit where under = @under";
            if (id + "" != "") sql = "select * from tblAccountLimit where under=@under and limitId <>" + id;
            SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@under", SqlDbType.Int).Value = under;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            if (ConnectionData._MyConnection.State == ConnectionState.Closed)
            {
                ConnectionData._MyConnection.Open();
            }
            da.Fill(table);
            cmd.Dispose();
            da.Dispose();
            return table;

        }
        public DataTable check_delete_Account(int limitId)
        {

            string sql = "SELECT * FROM tblAccountLimit  WHERE limitId = @limitId";
            SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@limitId", SqlDbType.Int).Value = limitId;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            if (ConnectionData._MyConnection.State == ConnectionState.Closed)
            {
                ConnectionData._MyConnection.Open();
            }
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;

        }
        public DataTable check_delete_clientregister(int limitId)
        {

            string sql = "SELECT * FROM tblClientRegister  WHERE accountlimitId = @limitId";
            SqlCommand cmd = new SqlCommand(sql, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@limitId", SqlDbType.Int).Value = limitId;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            if (ConnectionData._MyConnection.State == ConnectionState.Closed)
            {
                ConnectionData._MyConnection.Open();
            }
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

    }
}